package com.apimarket.util.kong.impl;

import com.apimarket.entity.kong.KeyAuthCredential;
import com.apimarket.factory.JsonMapperFactory;
import com.apimarket.util.UrlUtil;
import com.apimarket.util.kong.KongConsumer;
import com.apimarket.util.net.DeleteNetContext;
import com.apimarket.util.net.GetNetContext;
import com.apimarket.util.net.PostNetContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * Consumer与key-auth插件绑定实现类
 *
 * @author yyc
 */
@Component
public class ConsumerBindKeyAuth extends KongConsumer<KeyAuthCredential> {

    private static final String bindPath = "/key-auth";

    private static final String listPath = "/key-auths";

    private static final String listConsumerPath="/key-auth";

    public ConsumerBindKeyAuth() {
    }

    public ConsumerBindKeyAuth(String baseUrl) {
        super(baseUrl);
    }


    @Override
    public KeyAuthCredential bindPlugin(String nameOrId, Object... args) throws IOException, URISyntaxException {
        String targetPath = getBaseUrl() + bindPrefix + "/" + nameOrId + bindPath;
        /**官方推荐秘钥自生成，无需手动指定*/
        UrlUtil urlUtil = new UrlUtil(new PostNetContext(targetPath, null));
        JsonNode root = urlUtil.getJsonRoot();
        return JsonMapperFactory.getInstance().readValue(root.toString(), KeyAuthCredential.class);
    }

    @Override
    public List<KeyAuthCredential> listAllBindPluginDetail() throws IOException, URISyntaxException {
        String targetUrl = getBaseUrl() + listPath;
        return getCredentialList(targetUrl);
    }

    @Override
    public List<KeyAuthCredential> listBindPluginDetailViaConsumer(String nameOrId) throws IOException, URISyntaxException {
        String targetUrl = getBaseUrl() + bindPrefix + "/" + nameOrId + bindPath;
        return getCredentialList(targetUrl);
    }

    private List<KeyAuthCredential> getCredentialList(String targetUrl) throws IOException, URISyntaxException {
        UrlUtil urlUtil = new UrlUtil(new GetNetContext(targetUrl, null));
        JsonNode root = urlUtil.getJsonRoot();
        root = root.get("data");
        if (root == null) return null;
        ObjectMapper mapper = JsonMapperFactory.getInstance();
        CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, KeyAuthCredential.class);
        return mapper.readValue(root.toString(), listType);
    }

    public void delKeyAuth(String consumerId, String keyAuthId) throws IOException, URISyntaxException {
        String targetUrl = getBaseUrl() + bindPrefix + "/" + consumerId + bindPath+"/"+keyAuthId;
        UrlUtil urlUtil = new UrlUtil(new DeleteNetContext(targetUrl));
        urlUtil.getJsonRoot();
    }

    /*public List<KeyAuthCredential> getConsumerKeyAuth(String id) throws IOException, URISyntaxException {
        String targetUrl = getBaseUrl() + bindPrefix+"/"+id+listConsumerPath;
        UrlUtil urlUtil = new UrlUtil(new GetNetContext(targetUrl, null));
        JsonNode jsonRoot = urlUtil.getJsonRoot();
    }*/
}
